export function numberFormat(number, decimals, dec_point, thousands_sep) {
    /*
    * 参数说明：
    * number：要格式化的数字
    * decimals：保留几位小数
    * dec_point：小数点符号
    * thousands_sep：千分位符号
    * */
    number = (number + '').replace(/[^0-9+-Ee.]/g, '');
    var n = !isFinite(+number) ? 0 : +number,
        prec = !isFinite(+decimals) ? 0 : Math.abs(decimals),
        sep = (typeof thousands_sep === 'undefined') ? ',' : thousands_sep,
        dec = (typeof dec_point === 'undefined') ? '.' : dec_point,
        s = '',
        toFixedFix = function (n, prec) {
            var k = Math.pow(10, prec);
            return '' + Math.ceil(n * k) / k;
        };

    s = (prec ? toFixedFix(n, prec) : '' + Math.round(n)).split('.');
    var re = /(-?\d+)(\d{3})/;
    while (re.test(s[0])) {
        s[0] = s[0].replace(re, "$1" + sep + "$2");
    }

    if ((s[1] || '').length < prec) {
        s[1] = s[1] || '';
        s[1] += new Array(prec - s[1].length + 1).join('0');
    }
    return s.join(dec);
}
export function toThousands(num) {
  var num = (num || 0).toString(), result = '',
      tempNums = [], tempNum = 0, isFloat = num.indexOf('.') !== -1;
  if (isFloat) {
      tempNums = num.split('.');
      tempNum = tempNums[0].replace(/(^\s+)/g,'');
  } else {
      tempNum = num.replace(/(^\s+)/g,'');
  }
  while (tempNum.length > 3) {
          if(tempNum.slice(-4).substring(0,1) === '-'){
              result =  tempNum.slice(-3) + result;
          }else{
             result = ',' + tempNum.slice(-3) + result;
          }
      tempNum = tempNum.slice(0, tempNum.length - 3);
  }
  if (tempNum) {
      if (isFloat) {
          var tempNums_l = tempNums[1].length;
          if(tempNums_l > 2){
              tempNums[1] = tempNums[1].substr(0,2)
          }else if(tempNums[1].length < 2){
              tempNums[1] = tempNums[1]+'0'
          };
          result = tempNum + result + '.' + tempNums[1];
      } else {
          result = tempNum + result + '.00';
      }
  }
  return result;
}

/**
 * 解构带小数字符串
 * @param {*} val  需要计算的值
 */
export function countFloatNumber(val) {
    let tempVal = 0;
    let vals = []
    let len = 0
    if (val && val.indexOf('.') !== -1) {
        vals = val.split('.');
        len = vals[1].length;
        //tempVal = Number(val).toFixed(len);
        tempVal = val[0] + '.' + val[1]
    } else {
        tempVal = val + '.00';
    }
    return Number(tempVal);
}

export function accAdd(arg1, arg2) {
    var r1, r2, m, c;
    try {
        r1 = arg1.toString().split(".")[1].length;
    }
    catch (e) {
        r1 = 0;
    }
    try {
        r2 = arg2.toString().split(".")[1].length;
    }
    catch (e) {
        r2 = 0;
    }
    c = Math.abs(r1 - r2);
    m = Math.pow(10, Math.max(r1, r2));
    if (c > 0) {
        var cm = Math.pow(10, c);
        if (r1 > r2) {
            arg1 = Number(arg1.toString().replace(".", ""));
            arg2 = Number(arg2.toString().replace(".", "")) * cm;
        } else {
            arg1 = Number(arg1.toString().replace(".", "")) * cm;
            arg2 = Number(arg2.toString().replace(".", ""));
        }
    } else {
        arg1 = Number(arg1.toString().replace(".", ""));
        arg2 = Number(arg2.toString().replace(".", ""));
    }
    let tempSum = (arg1 + arg2) / m + '',
    prices = []

    if (tempSum.toString().indexOf('.') !== -1) {
        prices = tempSum.split('.')
       tempSum = prices[0] + '.' + prices[1].substring(0, 2)
    } else {
        tempSum = tempSum + '.00'
    }

    return Number(tempSum);
}
